<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/13 13:47
 * @version
 */
namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\MobileRecharge;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class MobileRechargeController extends Controller
{
    public function index()
    {
        return view('main::mobile_recharge', \Request::all());
    }

    public function all()
    {
        $data['items'] = MobileRecharge::orderBy('discount', 'desc')->orderBy('months', 'desc')->get();

        return success($data);
    }

    public function store(Request $request)
    {
        $data = $request->only([
            'title',
            'months',
            'discount',
            'tag',
            'min_amount',
            'max_amount',
            'can_use_coupon',
            'only_first',
        ]);

        $validator = Validator::make($request->all(), [
            'title' => 'required',
            'months' => 'required|numeric',
            'discount' => 'required',
            'min_amount' => 'required',
            'max_amount' => 'required',
        ]);

        if ($validator->fails()) {
            return fail(400, $validator->errors()->first());
        }
        $data['discount'] = floatval($data['discount']);
        if ($data['discount'] > 1 OR $data['discount'] <= 0) {
            return fail('400', '折扣格式错误');
        }

        MobileRecharge::create($data);

        return success();
    }

    public function update(Request $request, $id)
    {
        $res = MobileRecharge::find($id);
        try {

            if ($request->has('title') && $request->input('title')) {
                $res->title = $request->input('title');
            }
            if ($request->has('months') && $request->input('months')) {
                $res->months = $request->input('months');
            }
            if ($request->has('discount')) {
                $res->discount = $request->input('discount');
                if ($res->discount > 1 OR $res->discount <= 0) {
                    return fail('400', '折扣格式错误');
                }
            }
            if ($request->has('tag')) {
                $res->tag = $request->input('tag') ? : '';
            }
            if ($request->has('min_amount') && $request->input('min_amount')) {
                $res->min_amount = $request->input('min_amount');
            }
            if ($request->has('max_amount') && $request->input('max_amount')) {
                $res->max_amount = $request->input('max_amount');
            }
            if ($request->has('can_use_coupon')) {
                $res->can_use_coupon = $request->input('can_use_coupon') ? 1 : 0;
            }
            if ($request->has('only_first')) {
                $res->only_first = $request->input('only_first') ? 1 : 0;
            }

            $res->save();

            return success();
        } catch (\Exception $e) {
            Log::error($e);
            return fail(500, '系统出错了，请稍后再试');
        }
    }

    public function destroy($id)
    {
        $res = MobileRecharge::find($id);
        $res->delete();
        return success();
    }
}